//
//  WelcomeViewController.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/11/10.
//  Copyright © 2016年 wanglei. All rights reserved.
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "WelcomeViewController.h"
#import "UIlabel+extension.h"
#import "UIButton+extesion.h"
#import "AccountViewModel.h"
//注意uikit最好每次都引入一下,因为里面有很多最基本的视图布置的方法例如uicolor等
#import <UIKit/UIKit.h>
#import <Masonry/Masonry.h>
#import "UIImageView+WebCache.h"
#import "MainViewController.h"


@interface WelcomeViewController ()

@end


@implementation WelcomeViewController

-(void)loadView{
    
    [super loadView];
    
    self.view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ad_background"]];;
    
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self setUpUI];
    
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:YES];
    
    [self startAnimation];
}


- (void)startAnimation{
    
    CGFloat offset = -[UIScreen mainScreen].bounds.size.height + 160;
    
    [_iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(_iconView.superview.mas_bottom).offset(offset);
        
        
    }];
    
    
    /*   usingSpringWithDamping:弹簧动画的阻尼值，也就是相当于摩擦力的大小，该属性的值从0.0到1.0之间，越靠近0，阻尼越小，弹动的幅度越大，反之阻尼越大，弹动的幅度越小，如果大道一定程度，会出现弹不动的情况。换句话说， 范围 0~1 当它设置为1时，动画是平滑的没有振动的达到静止状态，越接近0 振动越大
     
     initialSpringVelocity:就是形变的速度，从视觉上看可以理解弹簧的形变速度，到动画结束，该速度减为0，所以，velocity速度越大，那么形变会越快，当然在同等时间内，速度的变化（就是速率）也会越快，因为速度最后都要到0。值越小弹簧的动力越小，弹簧拉伸的幅度越小，反之动力越大，弹簧拉伸的幅度越大。这里需要注意的是，如果设置为0，表示忽略该属性，由动画持续时间和阻尼计算动画的效果。  */
    [UIView animateWithDuration:2.0 delay:0 usingSpringWithDamping:0.98 initialSpringVelocity:9.8 options:UIViewAnimationOptionTransitionNone animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
        
//        此方法的不能满足要求
        //        [UIView animateWithDuration:1.0 animations:^{
        //            _welcomeLabel.alpha = 1;
        //
        //            MainViewController *mainviewcontrolle = [[MainViewController alloc]init];
        //
        //            [UIApplication sharedApplication].keyWindow.rootViewController = mainviewcontrolle;
        //
        //
        //        }];
        
        [UIView animateWithDuration:1.0 animations:^{
            
            _welcomeLabel.alpha = 1;
            
        } completion:^(BOOL finished) {
//            注意此处的控制器切换为主控制器,必须是在欢迎页面加载完毕之后,及在 _welcomeLabel.alpha = 1;完成之后写,因此此处要格外注意选用的动画方法
//            MainViewController *mainviewcontrolle = [[MainViewController alloc]init];
//            
//            [UIApplication sharedApplication].keyWindow.rootViewController = mainviewcontrolle;
            
            [[NSNotificationCenter defaultCenter] postNotificationName:KChoseRootVC object:@"welcome"];
            
            NSLog(@"come here welcome");
        }];
    }];
    
}

- (void)setUpUI{
    
    //    此处welcomeLabel,iconView必须使用self.的.语法,否则不会调用get方法,切记
    
    [self.view addSubview:self.welcomeLabel];
    
    [self.view addSubview:self.iconView];
    
    //    不设置MASAttachKeys时候,重复添加_iconView的约束时报错情况如下
    
    /*    "<MASLayoutConstraint:0x7fb838dc64b0 UIImageView:0x7fb838dc07e0.bottom == UIImageView:0x7fb838e0c2c0.bottom - 160>",
     "<MASLayoutConstraint:0x7fb838dc6d50 UIImageView:0x7fb838dc07e0.bottom == UIImageView:0x7fb838e0c2c0.bottom - 150>"  */
    
    //   设置MASAttachKeys时候,重复添加_iconView的约束时报错情况如下,很显然,设置MASAttachKeys可以更快的帮助我们找到问题所在
    
    /*
     "<MASLayoutConstraint:0x7fc603716a00 UIImageView:_iconView.bottom == UIImageView:0x7fc603713c60.bottom - 160>",
     "<MASLayoutConstraint:0x7fc60374e1e0 UIImageView:_iconView.bottom == UIImageView:0x7fc603713c60.bottom - 150>"
     */
    
    MASAttachKeys(_iconView,_welcomeLabel);
    
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_iconView.superview.mas_centerX);
        
        make.bottom.equalTo(_iconView.superview.mas_bottom).offset(-160);
        /*此处必须提前引入宏的设置才能确保不会报错#define MAS_SHORTHAND
         #define MAS_SHORTHAND_GLOBALS  http://www.jianshu.com/p/1841e6c69611*/
        
        NSValue *sizeValue = [NSValue valueWithCGSize:CGSizeMake(90, 90)];
        
        make.size.equalTo(sizeValue);
        
    }];
    
    
    [_welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //        注意对优先级进行设置时,优先级别的范围是 0--1000,priorityHigh()是750,priorityMedium()是500,priorityLow()是250,因此千万不要以为这里设置了priorityHigh()就是比其他没有设置的优先级高了,可能还有1000级别的优先级呢,还有就是优先级后面的()不能少,否则会报错
        
        make.centerX.equalTo(_welcomeLabel.superview.mas_centerX).offset(0);
        
        //        注意此处绝对不能用self.iconView,否则会报错 'couldn't find a common superview for <UIlabel_extension: 0x7ff79b71c050
        
        make.top.equalTo(_iconView.mas_bottom).offset(16);
        
    }];
    
    _welcomeLabel.alpha = 0;
    
    NSURL *url = [AccountViewModel sharedAccountModel].headImageURL;
#pragma mark 此处使用self.iconView 会报错
    [_iconView sd_setImageWithURL:url];
    
    _iconView.layer.cornerRadius = 45;
    
    _iconView.layer.masksToBounds = true;
}


//懒加载视图

- (UIImageView *)iconView{
    
    if(_iconView == nil){
        
        UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
        
        _iconView = imageview;
    }
    
    return _iconView;
    
}


- (UILabel *)welcomeLabel{
    
    UIlabel_extension *label = [[UIlabel_extension alloc]initWithtext:[NSString stringWithFormat:@"%@-欢迎归来",[AccountViewModel sharedAccountModel].userName] fontsize:19 textColor:[UIColor darkGrayColor] Alignment:NSTextAlignmentCenter];
    
    _welcomeLabel = label;
    
    return _welcomeLabel;
}

@end
